FUNCTION imageprofiler_manip::Init, $
    _REF_EXTRA=_extra

COMPILE_OPT idl2, HIDDEN

; Initialize the manipulator.
IF (~self->IDLitManipulator::Init( $
   NAME='Image Profiler', $
   TYPES=['IDLIMAGE'], $
   _EXTRA = _extra)) THEN $
      RETURN, 0

; Register the cursor of the manipulator.
self->imageprofiler_manip::DoRegisterCursor

; Indicate success if all has succeeded.
RETURN, 1
END

; ****************************************************************
; Configure the DoRegisterCursor method
; This method will create the cursor for the manipulator.
pro imageprofiler_manip::DoRegisterCursor

  compile_opt idl2, hidden

; Define the default cursor for this manipulation.
;  strArray = [ $
;        '       .        ', $
;        '      .#.       ', $
;        '     .###.      ', $
;        '    .#####.     ', $
;        '   ....#....    ', $
;        '.#.   .#. ...#. ', $
;        '.##.... ....##. ', $
;        '.#####   #####. ', $
;        '.##.... ....##. ', $
;        '.#.   .#.   .#. ', $
;        '   ....#....    ', $
;        '    .#####.     ', $
;        '     .###.      ', $
;        '      .#.       ', $
;        '       .        ', $
;        '                ']

  strArray = [ $
        '..              ', $
        '.#.             ', $
        '.##.            ', $
        '.###.           ', $
        '.####.          ', $
        '.#####.         ', $
        '.######.        ', $
        '.#######.       ', $
        '.########.      ', $
        '.#########.     ', $
        '.##########.    ', $
        '......###...    ', $
        '     .###.      ', $
        '      .###.     ', $
        '      .###.     ', $
        '       ....     ']

; Register the new cursor with the tool.
  self->RegisterCursor, strArray, 'LUT', /DEFAULT
END


pro imageprofiler_manip::OnMouseDown, oWin, x, y, iButton, $
    KeyMods, nClicks

	; Call our superclass.
	self->IDLitManipulator::OnMouseDown, $
	   oWin, x, y, iButton, KeyMods, nClicks

	; Return if there is no selection. Otherwise validate selection.
	IF (self.nSelectionList EQ 0) THEN $
	   RETURN
	self->SelectSinglePlaneImages, N_IMAGES=nImages

	; If no visualization meets requirements, return.
	IF ((self.nSelectionList EQ 0) && (nImages GT 0)) THEN BEGIN
	; Revert to default manipulator.
	   oTool = self->GetTool()
	   oTool->ActivateManipulator, /DEFAULT
	   RETURN
	ENDIF

	; Use the first image in the selection list as the
	; color table selection target. Note that GRID_DIMENSIONS
	; is a property of the undocumented IDLitVisImage class,
	; of which self.oImage is an instance.
	self.oImage = (*self.pSelectionList)[0]
	self.startx = x
	self.starty = y

end

pro imageprofiler_manip::OnMouseUp, oWin, x, y, iButton
	(self.oImage)->GetProperty, odata=odata
	ttt = odata->GetData(image)
	self.oImage->WindowToVis, self.startx, self.starty, dxs, dys
	self.oImage->WindowToVis, x, y, dxe, dye
	print,  self.startx, self.starty
	print, x, y
	print, dxs, dys
	print, dxe, dye
	print, extractprofile(image, dxs[0], dys[0], dxe[0] , dye[0])
end

function extractprofile, image, x, y, x1, y1
	s=size(image, /dimensions)
	sx = s[0] & sy=s[1]

	dx = float(x1-x)		;delta x
	dy = float(y1-y)
	n = abs(dx) > abs(dy)
	if n eq 0 then message, 'Zero length line.'
	;
	if abs(dx) gt abs(dy) then begin
		if x1 ge x then s=1 else s=-1
		sy = (y1-y)/abs(dx)
	   endif else begin
		if y1 ge y then sy=1 else sy=-1
		sx = (x1-x)/abs(dy)
	   endelse
	;
	xx = long(findgen(n+1l)*sx+x)	;X values, make into longwords.
	yy = long(findgen(n+1l)*sy+y)	;Y values
	return,image[long(yy)*sx + xx]
end

pro imageprofiler_manip::SelectSinglePlaneImages, $
    N_IMAGES=nImages

; If nothing is selected, return.
IF (self.nSelectionList EQ 0) THEN $
   RETURN

; Cull out multi-channel (RGB and RGBA) images from selection list.
; Cycle through the public instance pointer, pSelectionList,
; accessing all images. Use the number of selected items, contained
; in nSelectionList, to access each object in pSelectionList.
nValid = 0
nImages = 0
FOR i=0, self.nSelectionList-1 DO BEGIN
   oImage = (*self.pSelectionList)[i]
   IF (OBJ_ISA(oImage,'IDLitVisImage')) THEN BEGIN

      ; Increment nImages counter.
      nImages++

      ; Determine if the image is single plane. If so, add the
      ; image to the array of valid images and increment the
      ; nValid counter.
      oImage->GetProperty, N_IMAGE_PLANES=nImgPlanes
      IF (nImgPlanes EQ 1) THEN BEGIN
         validImgs=(nValid gt 0) ? [validImgs, oImage] : [oImage]
         nValid++
      ENDIF
   ENDIF
ENDFOR

IF (nValid GT 0) THEN BEGIN
   ; Store valid images using pSelectionList and nSelectionList.
   *self.pSelectionList = validImgs
   self.nSelectionList = nValid
ENDIF ELSE BEGIN
   ; If one or more images had been selected, but none are
   ; single-plane, then issue message.
   IF (nImages GT 0) THEN BEGIN
      self->ErrorMessage, $
      'Palettes can only be changed for single-plane images.', $
         TITLE='Color Table Manipulator Message', $
         SEVERITY=2
   ENDIF

   ; No images to manipulate - reset pSelectionList and    nSelectionList.
   void = TEMPORARY(*self.pSelectionList)
   self.nSelectionList = 0
   self.oImage = OBJ_NEW()
ENDELSE
END

pro imageprofiler_manip__define
	compile_opt idl2, hidden

	void = {imageprofiler_manip, $
			inherits IDLitManipulator, $
			oImage: obj_new(), $
			startx:0L, starty:0L,$
			endx:0L, endy:0L}

end